package d3_UDP2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("Server starting...");
        //服务端创建
        DatagramSocket socket = new DatagramSocket(7894);
        //创建接受数据包
        byte[] buf = new byte[1024*64];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        while (true) {
            //接收数据
            socket.receive(packet);
            int len = packet.getLength();
            String rs = new String(buf,0,len);
            System.out.println(rs);
            if(rs.equals("exit")){
                break;
            }
            String hs=  packet.getAddress().getHostAddress();
            int pt= packet.getPort();
            System.out.println("Server received: "+hs);
            System.out.println("Server received: "+pt);
            System.out.println("-------------------------------------------------------------------");
        }

        socket.close();
    }
}
